package com.lw.spring.utils;

import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/5 15:27
 */
public class BeanFactory {
    public static <T> T getService(Class<T> tClass) throws Exception{
//        UserService userService=new UserServiceImpl();
        T newInstance = tClass.newInstance();
        T o = (T) Proxy.newProxyInstance(tClass.getClassLoader(), tClass.getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                TransactionUtil.start();
                Object invoke = method.invoke(newInstance, objects);
                TransactionUtil.commit();
                LogUtils.addLog();
                return invoke;
            }
        });
        return o;
    }
}
